﻿#pragma once
#include<iostream>
#include <filesystem>
#include <fstream>
#include <stdint.h>
#include <memory>
#include <unordered_map>
#include <vector>
#include <base_type/bar_type.h>
#include <cppbase/datetime_type.h>
#include<map>



class FileStoreBase {
	
protected:
	std::string  dir;
	std::unordered_map<uint32_t, int32_t> tradingdate;//日期和id
	std::pair<uint32_t, uint32_t> trading_range;

	virtual std::filesystem::path get_filepath(Exchange exchangeid, std::string symbol, BarSize barsize) =0;
	virtual uint16_t row_size(BarSize barsize) =0;
	//url fast rename
	inline std::filesystem::path append_tmp(std::filesystem::path p);
	inline std::filesystem::path cut_tmp(std::filesystem::path p);
	bool check_file_valid(std::filesystem::path p);
	uint32_t today();
public:
	FileStoreBase(std::string rootpath);
	std::vector<char> read_bytes(Exchange exchangeid, std::string symbol, BarSize barsize);
	std::vector<char> read_bytes(std::filesystem::path p);
	void read_partial_bytes(std::filesystem::path p,int start,int length,char* buf);
	

	//virtual std::vector<DayBar> get_daybar(Exchange exchange, std::string symbol)=0;
	//virtual std::map<uint32_t, std::vector<MinuteBar>> get_minutebar(Exchange exchange, std::string symbol)=0;


	
};

inline std::filesystem::path FileStoreBase::append_tmp(std::filesystem::path p)
{	
	return p.parent_path()/(p.filename().string()+".tmp");
}
inline std::filesystem::path FileStoreBase::cut_tmp(std::filesystem::path p)
{
	auto name = p.filename().string();
	name.resize(name.size() - 4);//  .tmp
	return p.parent_path()/name;
}
